// pages/new-location-page/new-location-page.js
Page({
    data: {
        showLoadingState:false,
    // 场馆信息
        //图片
        p1:"https://pic.imgdb.cn/item/62da5797f54cd3f937994fd1.png",
        p4:"https://pic.imgdb.cn/item/62f9b06216f2c2beb183b2cc.png",
        p2:"https://pic.imgdb.cn/item/62da5799f54cd3f9379959d7.png",
        p14:"https://pic.imgdb.cn/item/62f4598b16f2c2beb185c3e8.png",
        p3:"https://pic.imgdb.cn/item/62f9b06216f2c2beb183b2be.png",
        p5:"https://pic.imgdb.cn/item/62da57a4f54cd3f937999196.png",
        p6:"https://pic.imgdb.cn/item/62da57a8f54cd3f93799a553.png",
        p16:"https://pic.imgdb.cn/item/62dfd88bf54cd3f937934a0d.png",
        p12:"https://pic.imgdb.cn/item/62da57c1f54cd3f9379a3037.png",
        //内容
        location:{
            location_id:123,
            location_name:'请输入名称',
            location:'请输入位置',
            location_type_room:0,
            location_type_free:0,
            location_open_time:9,
            location_close_time:10,
            location_sports:1,
            location_state:0,
        },
        //转换数组
        location_type_room_array:['室外','室内'],
        location_type_free_array:['付费','免费'],
        location_state_array:['下架','上架'],
        //picker数据
        type:[['室外','室内'],['付费','免费']],
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        type_index:[0,0],
        time_array:[['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00'],['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00']],
        time_array_:['8:00','9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00'],
        time_index:[0,0],//第二个数字代表偏移量-1，如果前面是6点，那么后面就是6+1+i；
    },
    onChangeLocationName:function(e){
        var that = this;
        wx.showModal({
          title:"请输入新名字",
          editable:true,
          success(res){
              if(res.confirm){
                that.setData({
                    [`location.location_name`]:res.content,
                })
              };
          }
        })
    },
    onChangeLocation:function(e){
        var that = this;
        wx.showModal({
          title:"请输入新地址",
          editable:true,
          success(res){
              if(res.confirm){
                that.setData({
                    [`location.location`]:res.content,
                })
              };
          }
        })
    },
    onChangeProperty:function(e){
        var x = e.detail.value;
        this.setData({
            [`location.location_type_room`]:x[0],
            [`location.location_type_free`]:x[1],
        })
    },
    onChangeSports:function(e){
        this.setData({
            [`location.location_sports`]:e.detail.value,
        })
    },
    onChangeTime:function(e){
        var x = e.detail.value;
        this.setData({
            [`location.location_open_time`]:x[0]+8,
            [`location.location_close_time`]:x[0]+x[1]+9,
            'time_index':e.detail.value,
        })
        //初始化数组
        var array = this.data.time_array_.slice();
        this.setData({
            'time_array[1]':array.splice(0,1),
        })
    },
    onBindTime:function(e){
        var array = this.data.time_array_.slice();
        array.splice(0,this.data.time_index[0]+1);
        this.setData({
            'time_array[1]':array,
        })
    },
    onColumnChange:function(e){
        var column = e.detail.column;
        if(column == 0){
            var array = this.data.time_array_.slice();
            array.splice(0,e.detail.value + 1);
            this.setData({
                'time_array[1]':array,
            })
        }
    },
    onChangeLocationState:function(e){
        this.data.location.location_state = (this.data.location.location_state+1)%2;
        this.setData({
            [`location.location_state`]:this.data.location.location_state,
        })
    },
    onConfirmAdd:function(e){
        var that = this;
        //提交代码到服务器
        that.onShowLoading();
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"CREATE_LOCATION",
              location_name:that.data.location.location_name,
              location:that.data.location.location,
              location_type_room:that.data.location.location_type_room,
              location_type_free:that.data.location.location_type_free,
              location_open_time:that.data.location.location_open_time,
              location_close_time:that.data.location.location_close_time,
              location_state:that.data.location.location_state,
              location_sports:that.data.location.location_sports,
              studentId:getApp().globalData.userInfo.studentId,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  that.onHideLoading();
                  console.log(res);
                  console.log({
                    //各种数据的传送
                    operation:"CREATE_LOCATION",
                    location_name:that.data.location.location_name,
                    location:that.data.location.location,
                    location_type_room:that.data.location.location_type_room,
                    location_type_free:that.data.location.location_type_free,
                    location_open_time:that.data.location.location_open_time,
                    location_close_time:that.data.location.location_close_time,
                    location_state:that.data.location.location_state,
                    location_sports:1,
                    studentId:getApp().globalData.userInfo.studentId,
                  });
                  wx.navigateBack({});
              }
            },
            fail(res){
                that.onHideLoading();
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
        });
        //返回到show-locations界面
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})